//
//  MZYnewsViewController.m
//  WristWatch
//
//  Created by dllo on 15/10/29.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "MZYnewsViewController.h"
#import "MZYHeader.h"
#import "MZYnewsTableViewCell.h"
#import "MZYnewsWebViewController.h"
#import "MZYnewTableViewCell.h"

@interface MZYnewsViewController ()<UITableViewDataSource, UITableViewDelegate>
@property(nonatomic, retain)UITableView *newsTableView;
@property(nonatomic, retain)UILabel *newsLabel;
@property(nonatomic, retain)NSMutableArray *newsArr;
@property(nonatomic, assign)NSInteger page;
@property(nonatomic, retain)MBProgressHUD *HUD;

@end

@implementation MZYnewsViewController

- (void)dealloc {
    [_newsArr release];
    [_newsLabel release];
    [_newsTableView release];
    [_HUD release];
    
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
     self.newsArr = [NSMutableArray array];
    [self createView];
    [self createData];
}

- (void)createView {
    self.newsTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT) style:UITableViewStylePlain];
    [self.view addSubview:self.newsTableView];
    [self.newsTableView release];
    self.newsTableView.rowHeight = 160;
    self.newsTableView.dataSource = self;
    self.newsTableView.delegate = self;
    
    //下拉
    self.newsTableView.header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(header)];
//    //上啦
//    self.newsTableView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(footer)];
//    
//    self.page = 1;

}

- (void)createData {
    AFHTTPRequestOperationManager *Tabmanager = [AFHTTPRequestOperationManager manager];
    [Tabmanager GET:@"http://api.watchnews.app887.com/api/Articles.action?opc=10&npc=0&type=%E8%A1%8C%E4%B8%9A%E8%B5%84%E8%AE%AF" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary *dic = responseObject;
        NSDictionary *mDic = dic[@"root"];
        self.newsArr = [MZYInformationModel MZYBaseModelByArr:mDic[@"list"]];
        
        if (self.newsArr.count > 0) {
            self.HUD.hidden = YES;
            [self.newsTableView reloadData];
        }
        [self.newsTableView.header endRefreshing];
        
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];

}

#pragma mark 上拉下拉方法
//下拉
- (void)header {
    //把已经刷新的数据删除,否则越刷新重复数据越多
    [self createData];
    
}
////上拉
//-(void)footer {
//    self.page += 2;
//    NSLog(@"%ld",(long)self.page);
//    NSString *url = [NSString stringWithFormat:@"http://api.watchnews.app887.com/api/Articles.action?opc=10&npc=0&type=%E8%A1%8C%E4%B8%9A%E8%B5%84%E8%AE%AF", (long)self.page];
//    
//    
//    // 解析UITableView
//    AFHTTPRequestOperationManager *Tabmanager = [AFHTTPRequestOperationManager manager];
//    [Tabmanager GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
//        NSDictionary *dic = responseObject;
//        NSDictionary *mDic = dic[@"root"];
//        self.newsArr = [MZYInformationModel MZYBaseModelByArr:mDic[@"list"]];
//        
//        if (self.newsArr.count > 0) {
//            self.HUD.hidden = YES;
//            [self.newsTableView reloadData];
//        }
//        [self.newsTableView.header endRefreshing];
//        
//    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
//        
//    }];
//}




- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.newsArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MZYInformationModel *info = self.newsArr[indexPath.row];
    if ([info.TYPESETTING  isEqual: @0]) {
        static NSString *reuse = @"reuse";
        MZYnewsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
        if (!cell) {
            cell = [[[MZYnewsTableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuse] autorelease];
        }
        cell.titleLabel.text = info.title;
        cell.timeLabel.text = [info.date substringToIndex:11];
        [cell.titleImageView sd_setImageWithURL:[NSURL URLWithString:info.imglink] placeholderImage:[UIImage imageNamed:@"3.jpg"]];
        return cell;
    } else {
        static NSString *reusea = @"reusea";
        MZYnewTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reusea];
        if (!cell) {
            cell = [[[MZYnewTableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reusea] autorelease];
        }
        cell.titleLabel.text = info.title;
        [cell.oneImageView sd_setImageWithURL:[NSURL URLWithString:info.imglink_1] placeholderImage:[UIImage imageNamed:@"3.jpg"]];
        [cell.twoImageView sd_setImageWithURL:[NSURL URLWithString:info.imglink_2] placeholderImage:[UIImage imageNamed:@"3.jpg"]];
        [cell.threeImageView sd_setImageWithURL:[NSURL URLWithString:info.imglink_3] placeholderImage:[UIImage imageNamed:@"3.jpg"]];
        return cell;
    }

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"我被点击了😠");
    MZYnewsWebViewController *newsWebVC = [[MZYnewsWebViewController alloc] init];
    newsWebVC.hidesBottomBarWhenPushed = YES;
    MZYInformationModel *info = self.newsArr[indexPath.row];
    newsWebVC.url = info.url;
    [self.navigationController pushViewController:newsWebVC animated:YES];
    [newsWebVC release];
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
